﻿Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Shapes
Imports Microsoft.Practices.EnterpriseLibrary.Data.Sql
Imports System.Data
Imports SILMConfig



''' <summary>
''' Interaction logic for LoginWindow.xaml
''' </summary>
Partial Public Class LoginWindow
    Inherits Window
    Public Sub New()
        Me.Cursor = Cursors.Wait
        InitializeComponent()
        txtUserName.Focus()
        Me.Cursor = Cursors.Arrow
    End Sub

    Private Sub CloseCommand_Executed(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
        Me.Close()
    End Sub

    Private Sub Window_MouseDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
        Me.DragMove()
    End Sub

    Private Sub btnLogin_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Me.Cursor = Cursors.Wait
        Dim state As Boolean = False
        Dim errorText As String = ""
        If Not (txtUserName.Text.Length > 0) Then
            errorText = ErrorMessages.[Default].UserNotValid
            state = True
        ElseIf Not (pbPassword.Password.Length > 0) Then
            errorText = ErrorMessages.[Default].UserNotValid
            state = True
        ElseIf Not (pbPassword.Password.Length >= 3) Then
            errorText = ErrorMessages.[Default].UserNotValid
            state = True
        End If

        If state = False Then
            Try
                Dim userName As String = txtUserName.Text
                Dim password As String = pbPassword.Password
                Dim encrypt_password As String = GlobalClass.GetMd5Hash(password)
                CheckUser(userName, encrypt_password)
            Catch

            End Try
        Else
            MessageBox.Show(errorText, "Error", MessageBoxButton.OK, MessageBoxImage.[Error])
        End If
        Me.Cursor = Cursors.Arrow
    End Sub

    Private Sub CheckUser(ByVal userName As String, ByVal password As String)
        Me.Cursor = Cursors.Wait
        GlobalClass._UserName = userName
        GlobalClass._Password = password
        GlobalClass._FullName = ""
        GlobalClass._PermissionID = 1
        GlobalClass._UserID = 1
        GlobalClass._Today = New Date()
        txtUserName.Text = ""
        pbPassword.Password = ""
        'this.DialogResult = true;


        'Defualt UserName and Password For Config
        'UserName=Config and Password=ConfigPass
        If userName = "Config" And password = "096E62FD294491F0D2E9C007012C9E94" Then
            txtUserName.Text = ""
            pbPassword.Password = ""
            'Shows Config Form
            Dim objConfigWindow As New ConfigWindow()
            objConfigWindow.Owner = Me
            Dim dg As Boolean = CBool(objConfigWindow.ShowDialog())
            If dg = True Then
                Me.DialogResult = False
            Else
                txtUserName.Focus()
            End If
            objConfigWindow = Nothing
        Else
            Dim objSqlDatabase As SqlDatabase = Nothing
            Dim objDatabaseClass As DatabaseClass = Nothing
            Try
                objSqlDatabase = New SqlDatabase(GlobalClass._ConStr)
                objDatabaseClass = New DatabaseClass(GlobalClass._ConStr)
                If objDatabaseClass.CheckConnection() Then
                    Dim spName As String = "Check_User"
                    Dim spParams As Object() = New Object(1) {}
                    spParams(0) = userName
                    spParams(1) = password
                    Dim ds As DataSet = objSqlDatabase.ExecuteDataSet(spName, spParams)
                    If ds.Tables(0).Rows.Count = 1 Then
                        If Convert.ToString(ds.Tables(0).Rows(0)("UserName")) = userName And Convert.ToString(ds.Tables(0).Rows(0)("Password")) = password Then
                            ' Waiter is not allowed to login into the Restaurant application
                            If Convert.ToInt32(ds.Tables(0).Rows(0)("PermissionID")) = 4 Then
                                MessageBox.Show(ErrorMessages.[Default].WaiterIsNotPermited, "Error", MessageBoxButton.OK, MessageBoxImage.[Error])
                                Me.DialogResult = False
                            Else
                                GlobalClass._UserName = userName
                                GlobalClass._Password = password
                                GlobalClass._FullName = Convert.ToString(ds.Tables(0).Rows(0)("FullName"))
                                GlobalClass._PermissionID = Convert.ToInt32(ds.Tables(0).Rows(0)("PermissionID"))
                                GlobalClass._UserID = Convert.ToInt32(ds.Tables(0).Rows(0)("UserID"))
                                GlobalClass._Today = Convert.ToDateTime(ds.Tables(0).Rows(0)("Today"))

                                txtUserName.Text = ""
                                pbPassword.Password = ""
                                'this.DialogResult = true;
                                Me.Hide()
                                'setDataBaseName()
                                'Dim objMainWindow As New MainWindow()
                                'Dim dg As System.Nullable(Of Boolean) = objMainWindow.ShowDialog()
                                'If dg = False Then
                                '    objMainWindow.Close()
                                '    Application.Current.Shutdown()
                                'End If
                                ArcGISRuntime.iniApps()
                                Dim objMainWindow As New Main()
                                objMainWindow.initProperties(userName)
                                SILMConfig.Settings.Default.ConnectionStr = Settings.Default.ConnectionStr
                                SILMConfig.Settings.Default.CurrentUser = userName
                                Dim dg As System.Nullable(Of Boolean) = objMainWindow.ShowDialog()
                                If dg = False Then
                                    objMainWindow.Close()
                                    Close()
                                End If
                            End If
                        Else
                            MessageBox.Show(ErrorMessages.[Default].UserNotValid, "Error", MessageBoxButton.OK, MessageBoxImage.[Error])
                            pbPassword.Password = ""
                            txtUserName.Focus()
                        End If
                    Else
                        MessageBox.Show(ErrorMessages.[Default].UserNotValid, "Error", MessageBoxButton.OK, MessageBoxImage.[Error])
                        pbPassword.Password = ""
                        txtUserName.Focus()
                    End If
                Else
                    MessageBox.Show(ErrorMessages.[Default].NoConnection, "Error", MessageBoxButton.OK, MessageBoxImage.[Error])
                    Me.DialogResult = False
                End If
            Catch ex As Exception
                MessageBox.Show(ErrorMessages.[Default].UnhandledException & " : " & ex.Message, "Error ", MessageBoxButton.OK, MessageBoxImage.[Error])
            End Try
        End If
        Me.Cursor = Cursors.Arrow

    End Sub
    'Public Sub setDataBaseName()
    '    Dim conStr As String = Settings.[Default].ConnectionStr
    '    Dim arrTemp As String() = conStr.Split(";"c)
    '    SILMConfig.DatabaseSettings.databaseName = arrTemp(1).Split("="c)(1)
    '    'MsgBox(SILM_V_06_2012.DatabaseSettings.databaseName)
    'End Sub

    Private Sub window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        CheckUser("Manager", GlobalClass.GetMd5Hash("Manager"))
    End Sub
End Class
